<template>
    <div id="classUserManagerContainer">
        <div id="headTitle">成员管理</div>
        <div id="search">
            <el-input id="searchTextInput" v-model="searchUser" placeholder="请输入搜索的用户名" style="width: 400px"/>
            <el-button type="primary" @click="loadClassUser">搜索</el-button>
        </div>
        <div id="table">
            <el-table max-height="700" :data="classUserList" border>
                <el-table-column prop="userID" label="用户ID" show-overflow-tooltip align="center"  width="100"/>
                <el-table-column prop="userName" label="用户名" show-overflow-tooltip align="center"/>
                <el-table-column label="操作" width="150" align="center">
                    <template #default="scope">
                        <el-button type="primary" plain @click="removeUser(scope.row.userID,scope.row.userName)">
                            移出班级
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <!--    分页  -->
        <div id="pagination">
            <el-pagination
                    v-model:current-page="pageNumber"
                    v-model:page-size="pageSize"
                    :page-sizes="[2, 5, 10, 15, 30, 50]"
                    :small=false
                    :disabled=false
                    :background=false
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="classSize"
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
            />
        </div>
    </div>
</template>
<script>
//css
import "../assets/ClassUserManager.css";
//请求
import request from '@/utils/requset.js';
//顶部提示信息
import prompt from '@/utils/prompt.js'
//路由跳转
import router from "@/router";

export default {
    name: 'classUserManager',
    data() {
        return {
            classUserList: [],
            removeUserData: {},
            classSize: 0,
            pageNumber: 1,
            pageSize: 2,
            searchUser: '',
        }
    },
    created() {
        if (this.isIntoClass() && this.isClassAdmin()) {
            this.loadClassUser();
        }
    },
    methods: {
        handleSizeChange(pageSize) {
            this.pageSize = pageSize;
            this.loadClassUser();
        },
        handleCurrentChange(pageNumber) {
            this.pageNumber = pageNumber;
            this.loadClassUser();
        },
        isIntoClass() {
            let classID = localStorage.getItem("userInfo") ? JSON.parse(localStorage.getItem("userInfo")).classID : 0;
            if (classID < 1) {
                prompt("还未加入班级！", "error");
                router.push({path: "home"});
                return false;
            }
            return true;
        },
        isClassAdmin() {
            let isClassAdmin = localStorage.getItem("userInfo") ? JSON.parse(localStorage.getItem("userInfo")).isClassAdmin : "";
            if (isClassAdmin === "是") {
                return true;
            }
            prompt("非班级管理员，权限不足！", "error");
            router.push({path: "home"});
            return false;
        },
        loadClassUser() {
            let token = localStorage.getItem("token") ? localStorage.getItem("token") : "";
            if (token === "") {
                prompt("还未登录！", "error");
                router.push({path: "login"});
                return;
            }
            request.get("/class/userList", {
                params: {
                    token: token,
                    pageNumber: this.pageNumber,
                    pageSize: this.pageSize,
                    searchUser: this.searchUser,
                }
            }).then(result => {
                //请求失败
                if (result.code !== "200") {
                    prompt(result.message, result.type);
                }
                //请求成功
                this.classSize = result.data.classSize;
                this.classUserList = result.data.classUserList;
            })
        },
        //移除班级成员
        removeUser(userID, userName) {
            this.removeUserData.userID = userID;
            this.removeUserData.userToken = localStorage.getItem("token") ? localStorage.getItem("token") : "";
            request.post(
                "/class/removeUser",
                this.removeUserData
            ).then(result => {
                if (result.code != "200") {
                    prompt(result.message, result.type);
                    return;
                }
                prompt("成功移出" + userName, result.type);
                router.push({path: "home"});
            });
        },
    }
}
</script>